package art.server.services;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import art.client.services.UserService;
import art.server.EntityConverter;
import art.server.db.DatabaseHandler;
import art.server.db.entities.PortalUser;
import art.server.db.entities.UserLevel;
import art.shared.entities.LoginInfo;
import art.shared.entities.UserShared;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {

	private static final long serialVersionUID = 1L;

	@Override
	public LoginInfo getLoginInfo() {
		return getUserFromSession();
		/*LoginInfo info = new LoginInfo();
		info.setEmailAddress("email");
		info.setLoggedIn(true);
		UserShared user = new UserShared();
		user.setEmail("email");
		user.setLevel(UserLevelShared.ADMIN);
		info.setUser(user);
		return info;*/
	}

	@Override
	public boolean deleteUser(UserShared user) {
		try {
			DatabaseHandler.getInstance().removeUser(user.getId());
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean updateUser(UserShared user) {
		try {
			DatabaseHandler.getInstance().saveUser(
					EntityConverter.getConverter().convertUser(user));
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public LoginInfo login(String email) {
		LoginInfo loginInfo = new LoginInfo();
		PortalUser user = DatabaseHandler.getInstance().getUserByEmail(email);
		if (user == null) {
			PortalUser newUser = new PortalUser();
			newUser.setLevel(UserLevel.NORMAL);
			newUser.setEmail(email);
			DatabaseHandler.getInstance().saveUser(newUser);
			newUser = DatabaseHandler.getInstance().getUserByEmail(email);
			loginInfo.setUser(EntityConverter.getConverter()
					.convertUserToShared(newUser));
		} else {
			loginInfo.setUser(EntityConverter.getConverter()
					.convertUserToShared(user));
			loginInfo.setEmailAddress(email);
			loginInfo.setLoggedIn(true);
		}
		storeUserInSession(loginInfo);
		return loginInfo;
	}

	@Override
	public boolean logout() {
		deleteUserFromSession();
		return true;
	}

	@Override
	public List<UserShared> getUsers() {
		List<PortalUser> users = DatabaseHandler.getInstance().getAllUsers();
		List<UserShared> usersShared = new ArrayList<UserShared>();
		if (users != null && !users.isEmpty()) {
			for (PortalUser user : users) {
				usersShared.add(EntityConverter.getConverter()
						.convertUserToShared(user));
			}
		}
		return usersShared;
	}

	private void storeUserInSession(LoginInfo loginInfo) {
		HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
		HttpSession session = httpServletRequest.getSession(true);
		session.setAttribute("user", loginInfo);
	}

	private void deleteUserFromSession() {
		HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
		HttpSession session = httpServletRequest.getSession();
		session.removeAttribute("user");
	}

	private LoginInfo getUserFromSession() {
		LoginInfo user = null;
		HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
		HttpSession session = httpServletRequest.getSession();
		Object userObj = session.getAttribute("user");
		if (userObj != null && userObj instanceof LoginInfo) {
			user = (LoginInfo) userObj;
		}
		return user;
	}
}
